#!/usr/bin/python
# -*- coding: utf-8 -*-

'''
Introduction to Programming in Java - An Interdisciplinary Approach

All book programs ported to Python by
Scott C. King (revisionx at {gee}ma1l <dot> c0m).

All program examples pass Pylint with a perfect 10.00 score.
Pylint:  http://www.pylint.org/


Program 1.4.1 - Sampling without replacement

Syntax:
python sample.py 10 1000


Notes:
int(sys.argv[1]) == Integer.parseInt(args[0])

sys is a required import here in order to use command line arguments.
https://docs.python.org/2/tutorial/stdlib.html

random is a required import as a substitute for Java's Math.random()
https://docs.python.org/2/library/random.html

'''

import sys
import random


def main():
    ''' Produces a random sample of integers from 0 to N-1 '''

    try:
        sample_size = int(sys.argv[1])
        sample_range = int(sys.argv[2])
    except IndexError:
        print 'Integer for sample size/range are required!'
        print 'Syntax: "python sample.py 10 1000"'
        sys.exit(0)

    perm = range(sample_range)  # creates "initialized" array

    # take sample
    for sample in range(sample_size):
        sample_index = sample + (int(random.random() * (sample_range - sample)))
        newval = perm[sample_index]
        perm[sample_index] = perm[sample]
        perm[sample] = newval

    # print sample
    for item in range(sample_size):
        print perm[item],


if __name__ == '__main__':
    main()
